/**
 * Created by yvdedu.com on 2016/10/20.
 */



//淡出
$(function () {
    $(".Article").css({opacity:1});
})

!function (){
    var url=location.href;

    var targetId=url.match(/user\\d*/)?url.match(/user\\d*/).substr(4):0;
    var page=1;
    $(window).scroll(function () {
        if($(window).height() + $(window).scrollTop()>=document.scrollingElement.scrollHeight&&$(window).scrollTop()>0){
            var articles=$(".userContent .Article");
            if(articles.length!=0&&articles.length%30==0){
                $.post("/getUserArticle",{page:++page,id:targetId}, function (o) {
                    if(o){
                        setArticle(JSON.parse(o));
                    }
                })
            }
        }
    });

    var model=$(".Article.scrollbar.model");
    function setArticle(js){
        for(var i=0;i<js.length;++i){
            var temp=model.clone(true);
            temp.find(".title.at").text(js[i].name);
            temp.find("p").html(js[i].content);
            temp.css("display","inline-block");
            $(".userContent").append(temp);
        }
    }


}();



//个人信息展示和编辑
!function(){
    var id=0;
    var editInfoPanelTarget=$(".editInfoPanel");
    var careTarget=$(".care");
    var editTarget=$(".editInfo");
    var funsTarget=$(".fans");
    var closeTarget=$(".closeEditInfo");
    var formTarget=$("#f");
    var infoMsgTarget=$(".infoMsg");
    var editPhotoTarget=$(".editPhoto");
    var fileIptTarget=$(".fileIpt");
    var nickNameETarget=$(".nickNameE");
    var ageETarget=$(".ageE");
    var ERoleTarget=$(".ERole");
    var introETarget=$(".introE");
    var editInfoBtnTarget=$(".editInfoBtn");
    var windowURL=window.URL||window.webkitURL;
    $(function(){
        getId();
        getFunsNum();
        isCare();
        getData();
    });

    function updateIptFile(){
        var file=fileIptTarget[0].files[0];
        var dataURL=windowURL.createObjectURL(file);
        editPhotoTarget.css("background-image","url("+dataURL+")")
    }

    function isCare() {
        $.post("/isCare",{id:id},function (o) {
            if (o==true){
                successCare();
            }else {
                falseCare();
            }
        })

    }


    //请求粉丝数量
    function getFunsNum(){
        $.post("/getFunsNum",{id:id},function(o){
            funsTarget.text("粉丝："+o);
        })
    }

    function open(){
        resetData();
        editInfoPanelTarget.fadeIn();
        getData();
    }

    function close(){
        editInfoPanelTarget.fadeOut();
    }


    function successCare(){
        changeCare("已关注","green","disabled");
    }
    function falseCare(){
        changeCare("关注","red",null);
    }

    function changeCare(text,color,able){
        careTarget.css("background-color",color);
        careTarget.text(text)
    }

    function getId(){
        id=funsTarget.attr("id");
        return id;
    }

    //请求关注
    function careUser(){
        getId();
        $.post("/care",{id:id},function(o){
            switch (o){
                case "noLogin":
                    $(".loginButton").click();
                    break;
                case "noUser":
                    // location.reload(true)
                    break;
                case "care":
                    successCare();
                    getFunsNum();
                    break;
                case "noCare":
                    falseCare();
                    getFunsNum();
                    break;
                default:
                    break;
            }
        })
    }

    //请求用户资料
    function getData(){
        $.post("/getUserInfo",{id:id},function(o){
            if(o){
                var js=JSON.parse(o);
                setData(js);
                setPanelData(js);
            }
        })
    }

    //提交修改
    function submit() {
        var f=new FormData(formTarget[0]);
        $.ajax({
            url: '/userInfoUp' ,
            type: 'POST',
            data: f,
            contentType: false,
            processData: false,
            success: function (o) {
                if(o!=null&&o.trim()!=""){
                    infoMsgTarget.text("修改成功");
                    infoMsgTarget.fadeIn(100);
                    editInfoPanelTarget.fadeOut(500);
                    var js=JSON.parse(o);
                    setPanelData(js);
                }
            },
            error: function (o) {
                console.log(o)
            }
        });
        return false;
    }

    function setPanelData(js) {
        var random=Math.random();
        $(".photo").attr("src",js.photo?js.photo+"?"+random:"/photo/0.png");
        $("div.name").text("昵称："+(js.nickName?js.nickName:js.name));
        $(".age").text("年龄："+js.age);
        $(".intro").text("个人介绍："+js.intro);
    }

    function setData(js){
        var random=Math.random();
        editPhotoTarget.css("background-image","url("+(js.photo?js.photo+"?"+random:"/photo/0.png")+"?"+random+")");
        fileIptTarget.val("");
        nickNameETarget.val(js.nickName);
        ageETarget.val(js.age);
        ERoleTarget.val(js.role);
        introETarget.val(js.intro);
    }

    function resetData(){
        editPhotoTarget.css("background-image","/photo/0.png");
        fileIptTarget.val("");
        nickNameETarget.val("");
        ageETarget.val("");
        ERoleTarget.val(0);
        introETarget.val("");
        infoMsgTarget.text("");
        infoMsgTarget.hide();
    }

    function careHover1() {
        var text=careTarget.text();
        if (text=="已关注"){
            changeCare("取消关注","red",null);
        }
    }

    function careHover2() {
        var text=careTarget.text();
        if(text=="取消关注"){
            successCare();
        }
    }

    careTarget.click(careUser);
    careTarget.mouseenter(careHover1);
    careTarget.mouseleave(careHover2);
    editTarget.click(open);
    closeTarget.click(close);

    fileIptTarget.change(updateIptFile);
    //todo:提交修改
    editInfoBtnTarget.click(submit);

}();

